package com.frogs.ucenter.service.daoimpl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.ucenter.dao.UserRechargeStrategyChooseDao;
import com.frogs.ucenter.entity.UserRechargeStrategyChoose;
import com.frogs.ucenter.service.UserRechargeStrategyChooseService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("userRechargeStrategyChooseService")
public class UserRechargeStrategyChooseServiceImpl extends BaseServiceImpl<UserRechargeStrategyChoose> implements UserRechargeStrategyChooseService {
   
	private static final Logger log = LoggerFactory.getLogger(UserRechargeStrategyChooseServiceImpl.class);
	
	@Autowired
    private UserRechargeStrategyChooseDao userRechargeStrategyChooseDao;

    public UserRechargeStrategyChooseServiceImpl() {
        super();
    }

    public UserRechargeStrategyChooseDao getUserRechargeStrategyChooseDao() {
        return userRechargeStrategyChooseDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<UserRechargeStrategyChoose> getBaseDao() {
        return userRechargeStrategyChooseDao;
    }

    public void setUserRechargeStrategyChooseDao(UserRechargeStrategyChooseDao userRechargeStrategyChooseDao) {
        this.userRechargeStrategyChooseDao = userRechargeStrategyChooseDao;
    }

	@Override
	public void insertLog(Map<String, Object> map) {
		try {
			userRechargeStrategyChooseDao.insertLog(map);
		} catch (Exception e) {
			log.error("func[insertLog] map[{}] exception[{} - {}] desc[fail]",
    				new Object[] { map.toString(), Arrays.deepToString(e.getStackTrace()) });
		}
	}

	@Override
	public void insert(Map<String, Object> map) {
		try {
			int typeId = Integer.parseInt(map.get("typeId").toString());
			int subId = Integer.parseInt(map.get("subId").toString());
			String expandCode = map.get("expandCode").toString();
			int applicablePlatformId = Integer.parseInt(map.get("applicablePlatformId").toString());
			//先删除
			Map<String,Object> params = new HashMap<String,Object> ();
			params.put("eqTypeId", typeId);
			params.put("eqExpandCode", expandCode);
			params.put("eqApplicablePlatformId", applicablePlatformId);
			userRechargeStrategyChooseDao.deleteByMap(params);
			//再添加
			UserRechargeStrategyChoose userRechargeStrategyChoose = new UserRechargeStrategyChoose();
			userRechargeStrategyChoose.setSubId(subId);
			userRechargeStrategyChoose.setTypeId(typeId);
			userRechargeStrategyChoose.setExpandCode(expandCode);
			userRechargeStrategyChoose.setApplicablePlatformId(applicablePlatformId);
			userRechargeStrategyChooseDao.insert(userRechargeStrategyChoose);
			//登记日志
			userRechargeStrategyChooseDao.insertLog(map);
		} catch (Exception e) {
			log.error("func[insert] map[{}] exception[{} - {}] desc[fail]",
    				new Object[] { map.toString(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
}